<template>
  <div class="article-toc">
    <div class="toc-header">
      <IconList class="icon" />
      <h3>目录</h3>
    </div>
    <div class="toc-content">
      <div
        v-for="item in tocItems"
        :key="item.id"
        :class="['toc-item', `level-${item.level}`, { active: activeId === item.id }]"
        @click="scrollToHeading(item.id)"
      >
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import IconList from '~/assets/icons/IconList.vue'

interface TocItem {
  id: string
  text: string
  level: number
}

const tocItems = ref<TocItem[]>([])
const activeId = ref('')

const extractToc = () => {
  const headings = document.querySelectorAll(
    '.markdown-body h2, .markdown-body h3, .markdown-body h4',
  )
  tocItems.value = Array.from(headings).map((heading, index) => {
    const id = `heading-${index}`
    heading.id = id
    return {
      id,
      text: heading.textContent || '',
      level: parseInt(heading.tagName.substring(1)),
    }
  })
}

const scrollToHeading = (id: string) => {
  const element = document.getElementById(id)
  if (element) {
    const offsetTop = element.offsetTop
    const offset = 80 // 预留顶部空间，避免被导航栏遮挡
    window.scrollTo({
      top: offsetTop - offset,
      behavior: 'smooth',
    })
  }
}

const updateActiveId = () => {
  const headings = document.querySelectorAll(
    '.markdown-body h2, .markdown-body h3, .markdown-body h4',
  )
  let currentId = ''

  headings.forEach((heading) => {
    const rect = heading.getBoundingClientRect()
    if (rect.top <= 100) {
      currentId = heading.id
    }
  })

  activeId.value = currentId
}

onMounted(() => {
  setTimeout(extractToc, 100)
  window.addEventListener('scroll', updateActiveId)
})

onUnmounted(() => {
  window.removeEventListener('scroll', updateActiveId)
})

defineExpose({ extractToc })
</script>

<style scoped>
.article-toc {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 100px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}

.toc-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f7fafc;
}

.toc-header .icon {
  width: 20px;
  height: 20px;
  color: #667eea;
}

.toc-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: #1a202c;
}

.toc-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.toc-item {
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  color: #4a5568;
  cursor: pointer;
  transition: all 0.3s;
  border-left: 2px solid transparent;
}

.toc-item:hover {
  background: #f7fafc;
  color: #667eea;
  border-left-color: #667eea;
}

.toc-item.active {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  color: #667eea;
  border-left-color: #667eea;
  font-weight: 600;
}

.toc-item.level-2 {
  padding-left: 12px;
}

.toc-item.level-3 {
  padding-left: 24px;
  font-size: 12px;
}

.toc-item.level-4 {
  padding-left: 36px;
  font-size: 12px;
  color: #718096;
}

/* 移动端样式 */
@media (max-width: 968px) {
  .article-toc {
    position: static;
    max-height: none;
  }
}
</style>
